package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppointRecordPojo;
import com.ruoyi.common.core.domain.entity.AppointmentVo;
import com.ruoyi.common.core.domain.entity.ServeApplyPojo;
import com.ruoyi.system.mapper.AppointRecordMapper;
import com.ruoyi.system.mapper.ServeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author czg
 * @date 2021/9/11 11:53 上午
 */
@Api(tags = "服务工作流程管理")
@RestController
@RequestMapping("/api/serve")
public class WorkFlowController {

    @Autowired
    ServeMapper serveMapper;


    @Autowired
    AppointRecordMapper appointRecordMapper;


    @Autowired
    RedisTemplate<String, String> redisTemplate;


    @PostMapping("/add")
    @ApiOperation("新增审核服务")
    public AjaxResult addServiceApply(@RequestBody ServeApplyPojo serveApplyPojo) {
        serveMapper.insert(serveApplyPojo);
        return AjaxResult.success();
    }

    @PutMapping("/audit")
    @ApiOperation("审批")
    public AjaxResult auditServiceApply(@RequestBody ServeApplyPojo serveApplyPojo) {
        serveMapper.updateById(serveApplyPojo);
        return AjaxResult.success();
    }

    @PostMapping("/appointment")
    @ApiOperation("预约")
    public AjaxResult appointment(@RequestBody AppointmentVo appointmentVo) {
        AppointRecordPojo appointRecordPojo = new AppointRecordPojo();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        appointRecordPojo.setDate(sdf.format(new Date()));
        appointRecordPojo.setTime(appointmentVo.getTime());
        appointRecordPojo.setCampus(appointmentVo.getCampus());
        appointRecordPojo.setLocationId(appointmentVo.getLocationid());
        appointRecordMapper.insert(appointRecordPojo);
        return AjaxResult.success();
    }

    @GetMapping("/appointment/list")
    @ApiOperation("用户预约记录")
    public AjaxResult<List<AppointRecordPojo>> appointmentList(HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = redisTemplate.opsForValue().get(token);
        QueryWrapper<AppointRecordPojo> que = new QueryWrapper<>();
        que.eq("user_id", userId);
        return AjaxResult.success(appointRecordMapper.selectList(que));
    }
}
